package base;

import android.app.Dialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;

import com.peng.auctionsystem.BuildConfig;
import com.peng.auctionsystem.R;

import manager.ActivityManager;
import mvp.BaseModel;
import mvp.BasePresenter;
import mvp.RxManager;
import utils.LogUtils;
import utils.ToastUtil;


/**
 * Created by Mr.Q on 2018/3/10.
 * 描述
 */

public abstract class BaseActivity<P extends BasePresenter, M extends BaseModel> extends AppCompatActivity {
    public P mPresenter;
    public M mModel;
    public BaseActivity mContext;
    public RxManager mRxManager;
    private static String LIFE_TAG = "BaseActivity";
    private Dialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        LIFE_TAG = getClass().getSimpleName();//获取子类实例的类名

        super.onCreate(savedInstanceState);
        //初始化RxBus、RxJava管理器
        mRxManager = new RxManager();
        //设置状态栏
        //doBeforeSetcontentView();

        getSupportActionBar().hide();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(getLayoutId());
        mContext = this;
        //        mPresenter = TUtil.getT(this, 0);
        //        mModel=TUtil.getT(this,1);

        this.initPresenter();
        if (mPresenter != null) {
            mPresenter.mContext = this;
        }
    }


    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        LogUtils.logd(LIFE_TAG + ":onRestoreInstanceState~~~");
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        LogUtils.logd(LIFE_TAG + ":onConfigurationChanged~~~");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        LogUtils.logd(LIFE_TAG + ":onRestart~~~");
    }

    @Override
    protected void onStart() {
        super.onStart();
        LogUtils.logd(LIFE_TAG + ":onStart~~~");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        LogUtils.logd(LIFE_TAG + ":onSaveInstanceState~~~");
    }

    /**
     * 设置layout前配置
     * 1、初始化主题
     * 2、Activity添加到Activity管理器
     * 3、设置状态栏的颜色，根据系统版本进行设置
     */
    private void doBeforeSetcontentView() {
        // 把actvity放到application栈中管理
        ActivityManager.getAppManager().addActivity(this);
        // 无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 默认着色状态栏
        //SetStatusBarColor();

    }

    /*********************
     * 子类实现
     *****************************/
    //获取布局文件
    public abstract int getLayoutId();

    /**
     * 简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
     */

    protected void initPresenter() {

    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
    }

    /**
     * 通过Class跳转界面，不含参数
     **/
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);

    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    @Override
    public void finish() {
        LogUtils.logd(LIFE_TAG + ":finish~~~");
        super.finish();
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    /**
     * finish当前界面
     *
     * @param closeAnim true：关闭时有动画，false：关闭是无动画
     */
    public void finish(boolean closeAnim) {
        super.finish();
        if (closeAnim) {
            overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        }
    }




    /**
     * 停止浮动加载进度条
     */
    public void stopProgressDialog() {
        if (mDialog != null) {
            mDialog.cancel();
        }
    }

    /**
     * 短暂显示Toast提示(来自String)
     **/
    public void showShortToast(String text) {
        ToastUtil.showShort(text);
    }

    /**
     * 短暂显示Toast提示(id)
     **/
    public void showShortToast(int resId) {
        ToastUtil.showShort(resId);
    }

    /**
     * 长时间显示Toast提示(来自res)
     **/
    public void showLongToast(int resId) {
        ToastUtil.showLong(resId);
    }

    /**
     * 长时间显示Toast提示(来自String)
     **/
    public void showLongToast(String text) {
        ToastUtil.showLong(text);
    }

    /**
     * 带图片的toast
     *
     * @param text
     * @param res
     */
    public void showToastWithImg(String text, int res) {
        ToastUtil.showToastWithImg(text, res);
    }

    /**
     * 网络访问错误提醒
     */
//    public void showNetErrorTip() {
//        ToastUtil.showToastWithImg(getText(R.string.net_error).toString(), R.drawable.msg_icon_fail);
//    }
//
//    public void showNoNetTip() {
//        ToastUtil.showToastWithImg(getText(R.string.no_net).toString(), R.drawable.msg_icon_fail);
//    }
//
//    public void showNetErrorTip(String error) {
//        ToastUtil.showToastWithImg(error, R.drawable.msg_icon_fail);
//    }

    @Override
    protected void onResume() {
        LogUtils.logd(LIFE_TAG + ":onResume~~~");
        super.onResume();
        //debug版本不统计crash
        if (!BuildConfig.LOG_DEBUG) {
            //友盟统计
        }
    }

    @Override
    protected void onPause() {
        LogUtils.logd(LIFE_TAG + ":onPause~~~");
        super.onPause();
        //debug版本不统计crash
        if (!BuildConfig.LOG_DEBUG) {
            //友盟统计
        }
    }


    @Override
    protected void onDestroy() {
        LogUtils.logd(LIFE_TAG + ":onDestroy~~~");
        ActivityManager.getAppManager().removeActivity(this);
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }
        mRxManager.clear();

    }

    protected void setFragment(@NonNull int containerViewId, BaseFragment fragment, String tag) {
        setFragment(containerViewId, fragment, null, tag);
    }

    /**
     * 为Activity中的容器设置Fragment
     * @param containerViewId
     * @param fragment
     * @param bundle
     * @param tag
     */
    protected void setFragment(@NonNull int containerViewId, BaseFragment fragment, Bundle bundle, String tag) {
        FragmentManager mFragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        //        transaction.addToBackStack(null);
        //        transaction.setCustomAnimations(R.animator.fragment_slide_right_enter, R.animator.fragment_slide_right_exit,R.animator.fragment_slide_right_enter, R.animator.fragment_slide_right_exit);
        //注意setCustomAnimations()方法必须在add、remove、replace调用之前被设置，否则不起作用。
        if (bundle != null) {
            fragment.setArguments(bundle);
        }
        transaction.add(containerViewId, fragment, tag);
        transaction.commit();
    }

}

